package com.chinapost.action;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.springframework.beans.factory.annotation.Autowired;

import com.chinapost.service.DeptServiceI;
import com.chinapost.service.ZxfServiceI;
import com.chinapost.vo.Json;
import com.chinapost.vo.SessionInfo;
import com.chinapost.vo.Zxf;
import com.opensymphony.xwork2.ModelDriven;

@Action(value = "zxfAction", results = { 
		@Result(name = "zxfManager", location = "/jsp/zxf/zxfManager.jsp"),
		@Result(name = "zxfAdd", location = "/jsp/zxf/zxfAdd.jsp"), 
		@Result(name = "zxfImport", location = "/jsp/zxf/zxfImport.jsp"),
		@Result(name = "zxfEdit", location = "/jsp/zxf/zxfEdit.jsp") 
		})
public class zxfAction extends BaseAction implements ModelDriven<Zxf>, ServletRequestAware {

	private Zxf zxf = new Zxf();
	private ZxfServiceI zxfService;
	private DeptServiceI deptService;
	private HttpServletRequest request;

	public DeptServiceI getDeptService() {
		return deptService;
	}

	@Autowired
	public void setDeptService(DeptServiceI deptService) {
		this.deptService = deptService;
	}

	public ZxfServiceI getZxfService() {
		return zxfService;
	}

	@Autowired
	public void setZxfService(ZxfServiceI zxfService) {
		this.zxfService = zxfService;
	}

	@Override
	public Zxf getModel() {
		return zxf;
	}

	public String zxfManager() {
		return "zxfManager";
	}

	public String zxfEdit() {
		return "zxfEdit";
	}

	public String zxfAdd() {
		return "zxfAdd";
	}

	public String zxfImport() {
		return "zxfImport";
	}


	public void add() {
		Json j = new Json();
		try {
			zxfService.add(zxf);
			j.setSuccess(true);
			j.setMsg("添加成功！");
		} catch (Exception e) {
			j.setMsg("添加失败！");
		}
		super.writeJson(j);
	}

	public void addZxfs() {
		Json j = new Json();
		int count = 0;
		try {
			count = zxfService.addZxfs(zxf);
			if (count > 0) {
				j.setSuccess(true);
				j.setMsg("上传成功，新增" + count + "条记录！");
			} else {
				j.setMsg("上传失败,新增0条记录！");
			}
		} catch (Exception e) {
			j.setMsg("上传失败,新增0条记录！");
		}
		super.writeJson(j);
	}

	public void edit() {
		Json j = new Json();
		try {
			zxfService.edit(zxf);
			j.setSuccess(true);
			j.setMsg("编辑成功!");
		} catch (Exception e) {
			j.setMsg("编辑失败！");
		}
		writeJson(j);
	}

	public void delete() {
		Json j = new Json();
		try {
			zxfService.delete(zxf);
			j.setSuccess(true);
			j.setMsg("删除成功！");
			j.setObj(zxf.getCid());
		} catch (Exception e) {
			j.setMsg("删除失败！");
		}
		super.writeJson(j);
	}

	public void datagrid() {
		if (!StringUtils.isEmpty(zxf.getDeptId())) {
			zxf.setDeptIds(deptService.getChildIds(zxf.getDeptId()));
		} else {
			SessionInfo s = (SessionInfo)request.getSession().getAttribute("sessionInfo");
			zxf.setDeptIds(deptService.getChildIds(s.getDeptid()));
		}
		super.writeJsonDate(zxfService.datagrid(zxf));
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;

	}
}
